package com.lottery.material.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableWebMvc
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket accessToken() {
        //添加head参数start
        ParameterBuilder appKey = new ParameterBuilder();
        ParameterBuilder timestamp = new ParameterBuilder();
        ParameterBuilder sign = new ParameterBuilder();
        ParameterBuilder deviceType = new ParameterBuilder();
        ParameterBuilder appVersion = new ParameterBuilder();

        List<Parameter> pars = new ArrayList<Parameter>();
        appKey.name("appKey").description("AppKey，由平台发放").modelRef(new ModelRef("string")).parameterType("body").required(true).defaultValue("hq1eLdlLiD7Wsp6XUxML9tIEK44bWGsq").build();
        timestamp.name("timestamp").description("时间戳，yyyy-mm-dd HH:MM:SS").modelRef(new ModelRef("string")).parameterType("body").required(true).defaultValue("123").build();
        deviceType.name("deviceType").description("设备类型（ios：苹果；android：安卓）").modelRef(new ModelRef("string")).parameterType("body").required(true).defaultValue("ios").build();
        appVersion.name("appVersion").description("版本号").modelRef(new ModelRef("string")).parameterType("body").required(true).defaultValue("V1.0").build();
        sign.name("sign").description("签名：MD5加密").modelRef(new ModelRef("string")).parameterType("body").required(true).build();
        pars.add(appKey.build());
        pars.add(timestamp.build());
        pars.add(sign.build());
        pars.add(deviceType.build());
        pars.add(appVersion.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("api")// 定义组
                .select() // 选择那些路径和 api 会生成 document
                .apis(RequestHandlerSelectors.basePackage("com.lottery.material.controller")) // 拦截的包路径
                .paths(PathSelectors.regex("/*/.*"))// 拦截的接口路径
                .build()
                .globalOperationParameters(pars)// 创建
                .apiInfo(apiInfo()); // 配置说明
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()//
                .title("聚料App接口文档")// 标题
                .description("聚料App接口文档")// 描述
//                .termsOfServiceUrl("")//
//                .contact(new Contact("", "", ""))// 联系
                .version("1.0")// 版本
                .build();
    }
}
